package com.umpay.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.umpay.dto.UMFResponse.Message;
import com.umpay.notify.bean.MerBackNotifyData;
import com.umpay.notify.util.MerBackNotifyUtil;

@Component
public class BackNotifyUtil {

    @Autowired
    private SignatureAuthUtil signatureAuthUtil;

    /**
     * 商户后台通知
     * 
     * @Description (TODO这里用一句话描述这个方法的作用)
     * @param Message
     * @param merId
     * @param url
     */
    public <T> void sendNotify(Message<T> Message, String merId, String url) {
        MerBackNotifyData merBackNotifyData = new MerBackNotifyData();
        merBackNotifyData.setMerId(merId);
        String data = getJsonString(Message);
        merBackNotifyData.setNotifyData(data);
        merBackNotifyData.setNotifyUrl(url);
        merBackNotifyData.setSign(signatureAuthUtil.signMsg(data));
        MerBackNotifyUtil.sendRestNotifyMsg(merBackNotifyData);

    }

    private static String getJsonString(Object obj) {
        String result = "";
        try {
            result = new ObjectMapper().writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return result;
    }
}
